#include "stdafx.h"
#include "public.h"
#include "kassistantbar.h"
#include "paintform.h"

KAssistantBar::KAssistantBar(QWidget *parent)
: SkinBaseWidget(parent)	
{
	setupUI();
}

void KAssistantBar::setupUI()
{
	//setWindowFlags(Qt::WindowStaysOnTopHint | Qt::SplashScreen);
	title = LoString(_T("KAssistant"));
	setWindowTitle(title);
	setWindowIcon(QIcon(":assistant.png"));
	m_titleIcon.load(":assistant.png");

	setFixedSize(215, 500);
	QRect rc = QApplication::desktop()->availableGeometry();	
	move(rc.right() - width() - 10, 200);	

	setMouseTracking(true);
	minOpcaity = 0.8F;
	maxOpcaity = 1.0F;
	windowOpacity = minOpcaity;
	setWindowOpacity(windowOpacity);
	isMouseEnter = false;
	effectTimer = new QTimer(this);		
	connect(effectTimer, SIGNAL(timeout()), this, SLOT(effectTimeEvent()));

}

bool KAssistantBar::event(QEvent *e)
{
	switch (e->type()) {
		case QEvent::Leave:
			leaveEffect();
			break;
		case QEvent::Enter:
			enterEffect();
			break;
		case QEvent::MouseMove:
			enterEffect();
			break;
	}
	return QWidget::event(e);
}

bool KAssistantBar::winEvent(MSG *message, long *result)
{
	switch(message->message)
	{
	case WA_INACTIVE:
	case WM_KILLFOCUS:
		{
			leaveEffect();
			break;
		}
	case WM_NCHITTEST:
		{
			enterEffect();

			const int captionHeight = 25;
			int w = width();
			int h = height();
			QPoint pos = mapFromGlobal(QCursor::pos());
			if (QRect(2, 2, w - 4, captionHeight - 2).contains(pos))
				*result = HTCAPTION;
			else
				*result = HTCLIENT;
			return true;
			break;
		}
	}
	return SkinBaseWidget::winEvent(message, result);
}

void KAssistantBar::leaveEffect()
{
	if(windowOpacity != minOpcaity && !geometry().contains(QCursor::pos()))
	{
		effectTimer->stop();
		isMouseEnter = false;			
		effectTimer->start(20);
	}
}

void KAssistantBar::enterEffect()
{
	if(windowOpacity != maxOpcaity)
	{
		effectTimer->stop();
		isMouseEnter = true;			
		effectTimer->start(20);
	}
}

void KAssistantBar::effectTimeEvent()
{
	if(isMouseEnter)
	{
		setWindowOpacity(windowOpacity);
		repaint();
		if(windowOpacity >= maxOpcaity)
		{
			windowOpacity = maxOpcaity;
			effectTimer->stop();
			return;
		}		
		windowOpacity = windowOpacity + 0.1F;
	}
	else
	{
		setWindowOpacity(windowOpacity);
		repaint();
		if(windowOpacity <= minOpcaity)
		{
			windowOpacity = minOpcaity;
			effectTimer->stop();
			return;
		}
		windowOpacity = windowOpacity - 0.1F;
	}
}